package online.sixfish.common.utils;

import online.sixfish.common.dto.UserDetail;
import online.sixfish.common.exception.ApiAsserts;

import online.sixfish.common.pojo.User;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

public final class SecurityUtil {
    public static User currentUser() {
        UserDetail userDetail = (UserDetail) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        if(Objects.isNull(userDetail)){
            ApiAsserts.fail("未进行认证");
        }
        return userDetail.getUser();
    }

    public static String currentUserId(){
       return currentUser().getId();
    }
}
